package helper.com.controller;

import helper.com.cache.anno.CacheDel;
import helper.com.cache.anno.CacheGet;
import helper.com.res.AjaxResponse;
import helper.com.service.ManagerService;
import helper.com.utils.TimeUtil;
import helper.com.vo.ManagerVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import static helper.com.constants.CacheConstants.MANAGER_KEY_PREFIX;

/**
 * @author 大忽悠
 * @create 2022/5/20 19:44
 */
@RestController
@Slf4j
@RequestMapping("/manager")
@RequiredArgsConstructor
public class ManagerController {
    private final ManagerService managerService;

    @PostMapping
    public AjaxResponse saveManager(@RequestBody ManagerVO managerVO){
       log.info("请求添加一个新的管理员: {}, 当前时间: {}",managerVO, TimeUtil.getCurTime());
       return managerService.saveManager(managerVO);
    }

    @CacheGet(key = MANAGER_KEY_PREFIX+"${account,id}")
    @GetMapping
    public AjaxResponse  getManager(@RequestParam String account,@RequestParam Long id){
        log.info("请求获取一个管理员: {}, 当前时间: {}",id, TimeUtil.getCurTime());
        return managerService.getManager(id);
    }

    @CacheDel(key = MANAGER_KEY_PREFIX+"${account,id}")
    @DeleteMapping
   public AjaxResponse delManager(@RequestParam String account,@RequestParam Long id){
        log.info("请求删除一个管理员: {}, 当前时间: {}",id, TimeUtil.getCurTime());
        return managerService.delManager(id);
    }
}
